﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LCA.Community.DAL.Model;
using LCA.Community.DAL.Interfaces;

namespace LCA.Community.DAL
{
    /// <summary>
    /// The repository for comments data manipulation.
    /// </summary>
    public class CommentsRepository : BaseRepository<Comment>, ICommentsRepository
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="CommentsRepository"/> class.
        /// </summary>
        /// <param name="dataServiceContext">The data service context.</param>
        public CommentsRepository(IDataServiceContext<Comment> dataServiceContext)
            :base(dataServiceContext)
        {
            
        }

        /// <summary>
        /// Gets the comments by post.
        /// </summary>
        /// <param name="postID">The post ID.</param>
        /// <returns>The comments of the post.</returns>
        public List<Comment> GetCommentsByPost(string postID)
        {
            return (from comment in Entities
                    where comment.Post == postID
                    select comment).OrderByDescending(c => c.Date).ToList();
        }

        /// <summary>
        /// Gets the comments by user.
        /// </summary>
        /// <param name="userID">The user ID.</param>
        /// <returns></returns>
        public List<Comment> GetCommentsByUser(string userID)
        {
            return (from comment in Entities
                    where comment.Author == userID
                    select comment).ToList();
        }

        /// <summary>
        /// Deletes all user comments.
        /// </summary>
        /// <param name="userID">The user ID.</param>
        public void DeleteAllUserComments(string userID)
        {
            var comments = GetCommentsByUser(userID);

            comments.ForEach(c => Delete(c));
        }

        /// <summary>
        /// Deletes all post comments.
        /// </summary>
        /// <param name="postID">The post ID.</param>
        public void DeleteAllPostComments(string postID)
        {
            var posts = GetCommentsByPost(postID);

            posts.ForEach(p => Delete(p));
        }
    }
}
